package com.training.api.controller;

import com.training.base.ResponseUtils;
import com.training.api.service.AppVersionService;
import com.training.base.Result;
import com.training.bean.AppVersion;
import com.training.utils.UrlUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/api/app")
public class AppVersionController {

    @Autowired
    private AppVersionService appVersionService;

    @Autowired
    private ResponseUtils responseUtils;

    /**
     * 获取最新的App版本信息
     *
     * @return 最新的App版本信息
     */
    @GetMapping("/version")
    public ResponseEntity<Result<AppVersion>> getLatestVersion(HttpServletRequest request) {
        try {
            AppVersion appVersion = appVersionService.getLatestVersion();
            appVersion.setFilePath(UrlUtils.buildFullUrl(request, appVersion.getFilePath()));
            if (appVersion != null) {
                return responseUtils.success(appVersion);
            } else {
                return responseUtils.failWithType(404, "暂无App版本信息");
            }
        } catch (Exception e) {
            return responseUtils.failWithType(500, "获取App版本信息失败: " + e.getMessage());
        }
    }
}